﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using JULONG.WEB.Library;
using DFYE.Web.Models;

namespace DFYE.Web.Areas.manage.Controllers
{
    using Models;

        public class userController : Controller
        {
            DataSession<entityDBContainer, manageUser> db = new DataSession<entityDBContainer, manageUser>();
            string defPassword = "123456".MD5();

            public userController()
            {
            }

            public ActionResult Index(string province)
            {
                ViewData.Model = db.query();
                return View();
            }

            //
            // GET: /manage/user/Create
            public ActionResult Create()
            {
                ViewData.Model = new manageUser();
                return View();
            }

            //
            // POST: /manage/user/Create

            [HttpPost]
            public ActionResult Create(manageUser manageUser)
            {
                if (db.query(d => d.loginname == manageUser.loginname).Any())
                {
                    ViewData.ModelState.AddModelError("loginname", "该登录名已经存在了！");
                    return View(manageUser);
                }

                manageUser.datetime = DateTime.Now;
                manageUser.password = defPassword;

                try
                {
                    // TODO: Add insert logic here
                    db.insert(manageUser);
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View(manageUser);
                }
            }

            //
            // GET: /manage/user/Edit/5

            public ActionResult Edit(int id)
            {
                ViewData.Model = db.get(d => d.ID == id);
                return View();
            }

            //
            // POST: /manage/user/Edit/5

            [HttpPost]
            public ActionResult Edit(int id, FormCollection collection)
            {
                var user = db.get(d => d.ID == id);
                try
                {
                    UpdateModel(user);
                    db.save();
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View(user);
                }
            }

            //
            // GET: /manage/user/Delete/5

            public ActionResult Delete(int id)
            {
                ViewData.Model = db.get(d => d.ID == id);
                return View();
            }

            //
            // POST: /manage/user/Delete/5

            [HttpPost]
            public ActionResult Delete(int id, FormCollection collection)
            {
                var user = db.get(d => d.ID == id);

                if (id == 1)
                {
                    ViewData.Model = user;
                    ViewData.ModelState.AddModelError("info", "不可以删除默认的系统管理员！");
                    return View();
                }

                if (id == AccountClass.manageUser.ID)
                {
                    ViewData.Model = user;
                    ViewData.ModelState.AddModelError("info", "不可以删除自己！");
                    return View();
                }

                if (user.requirement) {
                    ModelState.AddModelError("info", "该记录系统标记为不可删除");
                    return View(user);
                }

                try
                {
                    db.delete(user);
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }

            public ActionResult setPassword(int id)
            {
                ViewData.Model = db.get(d => d.ID == id);
                return View();
            }

            //
            // POST: /manage/user/Delete/5

            [HttpPost]
            public ActionResult setPassword(int id, FormCollection collection)
            {
                var user = db.get(d => d.ID == id);
                string newPsw = collection["psw"];

                if (String.IsNullOrWhiteSpace(newPsw)) { return RedirectToAction("setPassword", "user", new { id = id }); }

                try
                {
                    user.password = newPsw.MD5();
                    //user.password = defPassword;
                    db.save();
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View(user);
                }
            }

        }




}
